package view;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * This class renders the edit template view which can also be used to create new view
 * 
 * The edit filter panel contains buttons for adding/ updating and canceling operations, to, from, title
 * text fields, a body text area, and a combo box to select the destination folder.
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class EditFilterPanel extends JPanel {
	
	/**
	 * The buttons panel
	 */
	private JPanel buttonsPanel;

	/**
	 * Okay button
	 */
	private JButton addBtn, cancelBtn, updateBtn;
	
	/**
	
	 * @return the editBtn */
	public JButton getUpdateBtn() {
		return updateBtn;
	}

	/**
	 * To and from text fields
	 */
	private JTextField to, from, title,subject;
	
	/**
	 * Body field
	 */
	private JTextArea body;
	

	/**
	 * Move to folder combobox
	 */
	private JComboBox<String> moveToFolderComboBox;
	
	/**
	 * Constructor
	 */
	public EditFilterPanel(){
		
		super(new MigLayout("fillx"));
		initComponents();
		setup();
	}
	
	/**
	 * Sets the current mode, that is edit or create. This in turn hides the appropriate buttons
	 * @param mode "create" or "update"
	
	 */
	public void setMode(String mode){
		
		if (mode.equals("create")){
			buttonsPanel.remove(updateBtn);
			return;
		}
		if (mode.equals("update")){
			title.setEditable(false);
			buttonsPanel.remove(addBtn);
			return;
		}
	}

	/**
	 * Sets up the positioning in the panel
	 */
	private void setup() {
		
		
		this.add(new JLabel("Title: "));
		this.add(title, "width 400, wrap");
		title.setToolTipText("Title is the unique identifier for a filter");
		this.add(new JLabel("Move any emails that match the following... "), "span 2, wrap");
		
		this.add(new JLabel("Subject: "));
		this.add(subject, "width 400, wrap");
		this.add(new JLabel("To: "));
		this.add(to, "width 400, wrap");
		this.add(new JLabel("From: "));
		this.add(from, "width 400, wrap");
		this.add(new JLabel("Body: "));
		this.add(body, "width 400, height 50, wrap");
		this.add(new JLabel("Into the folder..."), "span 2, wrap");
		this.add(moveToFolderComboBox, "span 2, width 200,wrap");
		
		buttonsPanel = new JPanel(new MigLayout("fillx"));
		this.add(buttonsPanel, "skip 1, growx");
		
		buttonsPanel.add(addBtn, "align right, push");
		buttonsPanel.add(updateBtn, "align right, push");
		buttonsPanel.add(cancelBtn, "align right");

	}

	/**
	
	 * @return the cancelBtn */
	public JButton getCancelBtn() {
		return cancelBtn;
	}


	/**
	 * Initializes the components
	 */
	private void initComponents() {
		
		addBtn = new JButton("Add", IconsManager.getImageIcon("small/plus.png"));
		updateBtn = new JButton("Update", IconsManager.getImageIcon("small/tick.png"));
		cancelBtn = new JButton("Cancel", IconsManager.getImageIcon("small/cross.png"));
		to = new JTextField();
		from = new JTextField();
		body = new JTextArea();
		title=new JTextField();
		subject=new JTextField();
		moveToFolderComboBox = new JComboBox<String>();
	}

	/**
	
	 * @return the addBtn */
	public JButton getAddBtn() {
		return addBtn;
	}

	/**
	
	 * @return the title */
	public JTextField getTo() {
		return to;
	}

	/**
	
	 * @return the body */
	public JTextArea getBody() {
		return body;
	}

	/**
	
	 * @return the from */
	public JTextField getFrom() {
		return from;
	}

	/**
	
	 * @return the moveToFolderComboBox */
	public JComboBox<String> getMoveToFolderComboBox() {
		return moveToFolderComboBox;
	}

	/**
	 * 
	
	 * @return the title JText field */
	public JTextField getTitle() {
		return title;
	}


	/**
	 * Method setToText.
	 * @param to String
	 */
	public void setToText(String to) {
		this.to.setText(to);
	}

	/**
	 * Method setFromText.
	 * @param from String
	 */
	public void setFromText(String from) {
		this.from.setText(from);
	}

	/**
	 * Method setTitleText.
	 * @param title String
	 */
	public void setTitleText(String title) {
		this.title.setText(title);
	}

	/**
	 * Method setBodyText.
	 * @param body String
	 */
	public void setBodyText(String body) {
		this.body.setText(body);
	}

	/**
	 * Method to return the subject JText field
	 * @return
	 */
	public JTextField getSubject() {
		return subject;
	}

	/**
	 * Method to set the subjet text
	 * @param subject
	 */
	public void setSubjectText(String subject) {
		this.subject.setText(subject);
	}

	
}